//
// Dom7
// Export selectors engine


/*再来初始化APP*/
var $$ = Dom7;

// 初始化APP
var myApp = new Framework7({
    preprocess: preProcess,
    pushState: true,
    onPageBeforeAnimation: function (app, page) {
        document.title = titles[page.name];
    }
});


// 添加主界面
var mainView = myApp.addView('.view-main', {
    dynamicNavbar: true
});


/*全局设置ajax加载过程的动画*/
$$(document).on('ajaxStart', function (e) {
    if (e.detail.xhr.requestUrl.indexOf('autocomplete-languages.json') >= 0) {
        // Don't show preloader for autocomplete demo requests
        return;
    }
    myApp.showIndicator();
});

$$(document).on('ajaxComplete', function (e) {
    if (e.detail.xhr.requestUrl.indexOf('autocomplete-languages.json') >= 0) {
        // Don't show preloader for autocomplete demo requests
        return;
    }
    myApp.hideIndicator();
});


//全局变量，可以保存用户的信息
Template7.global = {
    username: '孙悟空',
    phone: '18198131234'
};

//toolbar的显示切换
$$('.tab').click(function () {
    $$('.tab.active').removeClass('active');
    $$(this).toggleClass('active')
});

//加载首页
mainView.router.load({
    url: './index/eat.html',
    animatePages: false
});

/**
 * 进入预约中心后，切换为指定的tab,在初始化之前显示，
 * 免得出现切换效果
 */
myApp.onPageBeforeInit("orders", function (page) {
    var pageIndex = $$('.tabs').attr('data-index');
    console.log('pageIndex: ', pageIndex);
    if (pageIndex === '2') {
        myApp.showTab('#tab3');
    } else if (pageIndex === '3') {
        myApp.showTab('#tab4');
    }
});

myApp.onPageBeforeInit('eat-page', function (page) {
    myApp.swiper('.swiper-container', {
        speed: 400,
        pagination: '.swiper-pagination',
        autoplay: 2500
    });
});
myApp.onPageBeforeInit('play-page', function () {
    myApp.swiper('.swiper-container', {
        speed: 400,
        pagination: '.swiper-pagination',
        autoplay: 2500
    });
});

/**
 * 进入预约界面，初始化日历控件
 */
myApp.onPageBeforeInit('order-page', function (page) {
    $.getScript('../static/custom/js/order/order-page.js')
});


/**
 * 商品详情界面，手动切换toolbar
 *
 * 还需要处理倒计时的工作
 */
myApp.onPageInit('goods-detail', function () {
    $.getScript('../static/custom/js/goods/details.js');
});

myApp.onPageBack('goods-detail', function () {
    myApp.showToolbar('#index-toolbar');
});


/**
 * 开启倒计时
 * 16 天13时 55 分
 */
myApp.onPageBeforeAnimation('goods-detail', function () {
    console.log('before Animation');
    myApp.hideToolbar('#index-toolbar');
    myApp.showToolbar('#goods-tools');
});


